package kr.pe.johnkim.app.mobile.rest.controller;

import kr.pe.johnkim.app.domain.MyHistoryItemEvent;
import kr.pe.johnkim.app.mobile.rest.service.MyHistoryItemEventService;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @author : johnkim <cale9797@gmail.com / Bettersoft Inc,.>
 * @Date : 12. 2. 18.
 */
@Controller
@Path("/myHistoryItemEventController")
public class MyHistoryItemEventController {
    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(MyHistoryItemEventController.class);

    @Autowired
    private MyHistoryItemEventService myHistoryItemEventService;

    /**
     * My History Item Event 를 등록 한다.
     * 
     * @param is
     * @return
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/registerItemEvent")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryItemEvent registerItemEvent(InputStream is) throws IOException, JSONException {
        logger.info("registerItemEvent");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonItemEvent = new JSONObject(line);
        JSONObject jsonItemEvent2 = jsonItemEvent.getJSONObject("myHistoryItemEvent");

        MyHistoryItemEvent parameterItemEvent = new MyHistoryItemEvent();

        parameterItemEvent.setComment(jsonItemEvent2.getString("comment"));
        parameterItemEvent.setAmount(jsonItemEvent2.getString("amount"));
        parameterItemEvent.setOpenItemEventYn(jsonItemEvent2.getString("openItemEventYn"));
        parameterItemEvent.setFavoriteYn(jsonItemEvent2.getString("favoriteYn"));
        parameterItemEvent.setActivateYn(jsonItemEvent2.getString("activateYn"));
        parameterItemEvent.setModifyDate(jsonItemEvent2.getString("modifyDate"));
        parameterItemEvent.setRegisterDate(jsonItemEvent2.getString("registerDate"));
        parameterItemEvent.setMyHistoryMembersId(jsonItemEvent2.getString("myHistoryMembersId"));
        parameterItemEvent.setMyHistoryItemsIdMyHistoryItems(jsonItemEvent2.getString("myHistoryItemsIdMyHistoryItems"));

        MyHistoryItemEvent resultItemEvent = new MyHistoryItemEvent();

        if(myHistoryItemEventService.registerItemEvent(parameterItemEvent)) {
            resultItemEvent.setResult("SUCCESS");
        } else {
            resultItemEvent.setResult("FAIL");
        }

        return resultItemEvent;
    }

    /**
     * My History Item Event 목록을 가져 온다.
     * 
     * @param is
     * @return
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/selectItemEventList")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public List<MyHistoryItemEvent> selectItemEventList(InputStream is) throws IOException, JSONException {
        logger.info("selectItemEventList");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonItemEvent = new JSONObject(line);
        JSONObject jsonItemEvent2 = jsonItemEvent.getJSONObject("myHistoryItemEvent");

        MyHistoryItemEvent parameterItemEvent = new MyHistoryItemEvent();

        parameterItemEvent.setActivateYn(jsonItemEvent2.getString("activateYn"));
        parameterItemEvent.setMyHistoryMembersId(jsonItemEvent2.getString("myHistoryMembersId"));
//        parameterItemEvent.setMyHistoryItemsIdMyHistoryItems(jsonItemEvent2.getString("myHistoryItemsIdMyHistoryItems"));

        return myHistoryItemEventService.selectItemEventList(parameterItemEvent);
    }

    /**
     * My History Item Event 를 수정 한다.
     * 
     * @param is
     * @return
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/updateItemEvent")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryItemEvent updateItemEvent(InputStream is) throws IOException, JSONException {
        logger.info("updateItemEvent");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonItemEvent = new JSONObject(line);
        JSONObject jsonItemEvent2 = jsonItemEvent.getJSONObject("myHistoryItemEvent");

        MyHistoryItemEvent parameterItemEvent = new MyHistoryItemEvent();

        parameterItemEvent.setIdMyHistoryItemEvents(jsonItemEvent2.getString("idMyHistoryItemEvents"));
        parameterItemEvent.setComment(jsonItemEvent2.getString("comment"));
        parameterItemEvent.setAmount(jsonItemEvent2.getString("amount"));
        parameterItemEvent.setOpenItemEventYn(jsonItemEvent2.getString("openItemEventYn"));
        parameterItemEvent.setFavoriteYn(jsonItemEvent2.getString("favoriteYn"));
        parameterItemEvent.setActivateYn(jsonItemEvent2.getString("activateYn"));
        parameterItemEvent.setModifyDate(jsonItemEvent2.getString("modifyDate"));
        parameterItemEvent.setMyHistoryMembersId(jsonItemEvent2.getString("myHistoryMembersId"));
        parameterItemEvent.setMyHistoryItemsIdMyHistoryItems(jsonItemEvent2.getString("myHistoryItemsIdMyHistoryItems"));

        MyHistoryItemEvent resultItemEvent = new MyHistoryItemEvent();

        if(myHistoryItemEventService.updateItemEvent(parameterItemEvent)) {
            resultItemEvent.setResult("SUCCESS");
        } else {
            resultItemEvent.setResult("FAIL");
        }

        return resultItemEvent;
    }

    /**
     * My History Item Event 를 비활성화 한다.
     * 
     * @param is
     * @return
     * @throws IOException
     * @throws JSONException
     */
    @POST
    @Path("/updateDeactivateItemEvent")
    @Consumes({"application/json", "application/xml"})
    @Produces({"application/json", "application/xml"})
    public MyHistoryItemEvent updateDeactivateItemEvent(InputStream is) throws IOException, JSONException {
        logger.info("updateDeactivateItemEvent");

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();

        logger.info("line : {}", line);

        JSONObject jsonItemEvent = new JSONObject(line);
        JSONObject jsonItemEvent2 = jsonItemEvent.getJSONObject("myHistoryItemEvent");

        MyHistoryItemEvent parameterItemEvent = new MyHistoryItemEvent();

        parameterItemEvent.setActivateYn(jsonItemEvent2.getString("activateYn"));
        parameterItemEvent.setModifyDate(jsonItemEvent2.getString("modifyDate"));
        parameterItemEvent.setMyHistoryMembersId(jsonItemEvent2.getString("myHistoryMembersId"));
        parameterItemEvent.setMyHistoryItemsIdMyHistoryItems(jsonItemEvent2.getString("myHistoryItemsIdMyHistoryItems"));

        MyHistoryItemEvent resultItemEvent = new MyHistoryItemEvent();

        if(myHistoryItemEventService.updateDeactivateItemEvent(parameterItemEvent)) {
            resultItemEvent.setResult("SUCCESS");
        } else {
            resultItemEvent.setResult("FAIL");
        }

        return resultItemEvent;
    }
}
